import React from 'react'
import { Route, IndexRoute } from 'react-router'

import * as auth from './utils/auth'


function requireLogin(nextState, replace){
    if (!auth.loggedIn()) {
        replace({
            pathname: '/login',
            state: { nextPathname: nextState.location.pathname }
        })
    }
}

function logout(){

}

function checkUserConfigureStatus(){
    if(auth.getUserConfigureStatus() == null){
        auth.fetchUserConfigureStatus().then(json => {
            if(json.body && json.body.configureStatus){
                auth.setUserConfigureStatus(json.body.configureStatus);
            }
            return auth.getUserConfigureStatus() == '1'
        })
        .catch(err => {
            return false
        });
    }else{
        return auth.getUserConfigureStatus() == '1'
    }
}
const routes = {
    component: require('./layout').default,
    childRoutes:[
        {
            path:'/',
            onEnter: requireLogin,
            getComponent:(nextState, cb) => {
                console.log('root')
                require.ensure([],(require) => {
                    cb(null, require('./modules/index').default)
                })
            },
            indexRoute: {
                getComponent: (nextState, cb) => {
                    console.log('index')
                    if(checkUserConfigureStatus()){
                        require.ensure([],(require) => {
                            cb(null, require('./modules/assetsConfig/assetsConfig').default)
                        })
                    }else {
                        require.ensure([],(require) => {
                            cb(null, require('./modules/guideline').default)
                        })
                    }
                }
            },
            childRoutes: [
                require('./modules/dialogue').default,
                require('./modules/quickConfig').default,
                require('./modules/discover').default
                // require('./modules/modified').default,
                // require('./modules/productOrder').default,
                // require('./modules/profits').default,
                // require('./modules/questionnaire').default
            ]
        },
        {
            path:'/login',
            getComponent: (nextState, cb) => {
                require.ensure(['./modules/login/login'],(require) => {
                    cb(null, require('./modules/login/login').default)
                })
            }
        },
        {
            path:'/loginSuccess',
            getComponent: (nextState, cb) => {
                require.ensure(['./modules/login/success'],(require) => {
                    cb(null, require('./modules/login/success').default)
                })
            }
        },
        // {
        //     path:'/logout',
        //     getComponent: (nextState, cb) => {
        //         return require.ensure([],(require) => {
        //             cb(null, require('./modules/logout'))
        //         })
        //     }
        // },
        {
            path:'*',
            getComponent: (nestState, cb) => {
                return require.ensure([],(require) => {
                    cb(null, require('./modules/notfound').default)
                })
            }
        }
    ]
}

// import CoreLayout from './layout'
// import Home from './modules/home'
// import Guideline from './modules/guideline'
// import AssetsConfig from './modules/assetsConfig'
// import Discover from './modules/discover/discover'
// import AssetsReport from './modules/discover/assetsReport'
// import Balance from './modules/discover/balance'
// import Dialogue from './modules/dialogue'
// import Profits from './modules/profits'
// import Modified from './modules/modified'
// import Questionnaire from './modules/questionnaire'
// import ProductOrder from './modules/productOrder/productOrder'
// import ProductSelect from './modules/productOrder/productSelect'
// import Login from './modules/login'
// import NotFound from './modules/notfound'
//
//
// function requireCredentials(nextState, replaceState){
//     let query = nextState.location.query;
//     let uid = loggedIn();
//     console.log('uid:'+uid)
//     // let isLoginIn = Boolean(uid)
//     let isLoginIn = true
//
//     if (!isLoginIn) {
//         replaceState({
//             pathname: '/login',
//             state: { nextPathname: nextState.location.pathname }
//         })
//     }
// }
//
// const routes = (
//     <Route component={CoreLayout}>
//         <IndexRoute component={Home}/>
//         <Route path="modules" component={Home} onEnter={requireCredentials}>
//             <Route path="guideline" component={Guideline}/>
//             <Route path="assetsConfig" component={AssetsConfig}/>
//
//             <Route path="discover" component={Discover}>
//             </Route>
//             <Route path="assetsReport" component={AssetsReport} />
//
//             <Route path="balance" component={Balance} />
//
//             <Route path="whitepaper">
//             </Route>
//             <Route path="whitepaper/:id" />
//
//             <Route path="dialogue" component={Dialogue}/>
//
//             <Route path="profits" component={Profits}/>
//
//             <Route path="modified" component={Modified}/>
//
//             <Route path="freeUser" component={Questionnaire}/>
//
//             <Route path="productOrder" component={ProductOrder} />
//             <Route path="productSelect" component={ProductSelect} />
//             <Route path="modules">
//                 <Route path="productOrder/productOrder.html" component={ProductOrder} />
//             </Route>
//         </Route>
//
//         {/* 登录 */}
//         <Route path="login" component={Login} />
//
//         {/* 404 */}
//         <Route path="**" component={NotFound} />
//     </Route>
// )



export default routes
